//
//  ViewController.m
//  Exam11-1
//
//  Created by 孙晓晔 on 2017/4/14.
//  Copyright © 2017年 孙晓晔. All rights reserved.
//

#import "ViewController.h"
#import "MyFile.h"

@interface ViewController ()
@property(nonatomic,strong) MyFile *file;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    NSString *strTemp = NSTemporaryDirectory();
    
    NSLog(strTemp,nil);
    
    self.file = [[MyFile alloc]initWithDirectory:strTemp];
}

- (IBAction)createDir:(id)sender {
    
    if ([self.file createDir:@"/bb/22"]) {
        NSLog(@"/bb/22目录创建成功。", nil);
    }
}

- (IBAction)createFile:(id)sender {
    
    if ([self.file createFile:@"test.txt"]) {
        NSLog(@"创建test.txt文件成功。", nil);
    }
}

- (IBAction)writeFile:(id)sender {
    if ([self.file writeFile:@"str.txt" str:@"测试信息。"]) {
        NSLog(@"字符串写str.txt文件成功。", nil);
    }
    
    NSDictionary *dict = @{@"company":@"极客学院", @"student":@"sunxy"};
    if ([self.file writeFile:@"dict.txt" dict:dict]) {
        NSLog(@"字典写dict.txt文件成功。", nil);
    }
    
    NSArray *arr = @[@"aa", @"bb", @"cc"];
    if ([self.file writeFile:@"array.txt" arr:arr]) {
        NSLog(@"数组写array.txt文件成功。", nil);
    }
}

- (IBAction)readFile:(id)sender {
    
    NSString *str = [self.file readFile:@"test.txt"];
    
    NSLog(@"test.txt文件内容 = %@", str);
}

- (IBAction)listFile:(id)sender {
    NSString *path = NSTemporaryDirectory();
    
    NSArray *arr = [self.file listAtPath:path];
    
    for (int i = 0; i < [arr count]; i++) {
        NSLog(@"%@", arr[i]);
    }
}

- (IBAction)isExistFile:(id)sender {
    
    if ([self.file isExistAtPath:@"dict.txt"]) {
        NSLog(@"dict.txt文件存在");
    }
}

- (IBAction)sizeFile:(id)sender {
    unsigned long long size = [self.file fileSizeAtPath:@"dict.txt"];
    
    NSLog(@"dict.txt文件大小是 %llu", size);
}

- (IBAction)sizeDir:(id)sender {
    
    NSString *path = NSTemporaryDirectory();
    
    unsigned long long size = [self.file folderSizeAtPath:path];
    
    NSLog(@"tmp目录下所有文件大小是 %llu", size);
}

- (IBAction)deleteFile:(id)sender {
    
    if ([self.file deleteFile:@"test.txt"]) {
        NSLog(@"test.txt文件删除成功。", nil);
    }
}
- (IBAction)moveFile:(id)sender {
    
    NSString *home = NSHomeDirectory();
    NSString *path = NSTemporaryDirectory();
    
    NSString *srcFile = [path stringByAppendingString:@"test.txt"];
    NSString *dstFile = [home stringByAppendingString:@"test.txt"];
    
    if ([self.file moveFile:srcFile dstPath:dstFile]) {
        NSLog(@"test.txt文件移动至home目录成功。", nil);
    }
}
@end
